package hku.cs.lunuo.rank;

import hku.cs.lunuo.constant.DirAndFileConstant;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public abstract class StatisticProvider {
	protected List<String> args;
	protected File dataDir;
	
	public HashMap<String, Boolean> readResultData()
			throws JDOMException, IOException {
		SAXBuilder sb = new SAXBuilder();
		Document xmlDoc = sb.build(new File(dataDir, DirAndFileConstant.RESULT
				+ DirAndFileConstant.FILEEXTENTION));
		HashMap<String, Boolean> testResult = new HashMap<String, Boolean>();
		Element xmlRoot = xmlDoc.getRootElement();
		Iterator xmlTestMethodIt = xmlRoot.getChildren("testmethod").iterator();

		while (xmlTestMethodIt.hasNext()) {
			Element xmlTestMethod = (Element) xmlTestMethodIt.next();
			String name = xmlTestMethod.getAttributeValue("name");
			if (args == null || args.contains(name)) {
				String tmp = xmlTestMethod.getAttributeValue("status");
				boolean b = Boolean.parseBoolean(tmp);
				testResult.put(name, b);
			}
		}
		return testResult;
	}
	
	public Statistic provideData(File file, List<String> args) throws Exception {
		this.dataDir = file;
		this.args = args;
		HashMap<String, Boolean> testResult = readResultData();
		return readStatisticData(testResult);
	}

	public abstract Statistic readStatisticData(HashMap<String, Boolean> testResult) throws Exception;
}